package com.asj.vivo.lock.annotation;

import static com.asj.enums.AppIdentifierDictEnums.KEY_ALL_OPERATIONAL_INTERFACES_OP_NAME;
import static com.asj.enums.AppIdentifierDictEnums.KEY_ALL_OPERATIONAL_INTERFACES_OP_REMARK;
import static com.asj.enums.AppIdentifierDictEnums.KEY_ALL_OPERATIONAL_INTERFACES_OP_TYPE;

import android.util.Log;

import com.google.gson.JsonObject;

import org.json.JSONException;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class AnnotationProcessor {
    private static final String TAG = "MyActivity";

    public static List<JsonObject> getAnnotatedMethodsInfo(Class<?> clazz) throws JSONException {
        List<JsonObject> methodRemarks = new ArrayList<>();

        Method[] methods = clazz.getDeclaredMethods(); // 获取类的所有方法
        for (Method method : methods) {
            if (method.isAnnotationPresent(MethodRemark.class)) {
                MethodRemark remark = method.getAnnotation(MethodRemark.class);
                assert remark != null;
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty(KEY_ALL_OPERATIONAL_INTERFACES_OP_NAME.getValue(),method.getName());
                jsonObject.addProperty(KEY_ALL_OPERATIONAL_INTERFACES_OP_REMARK.getValue(),remark.remark());
                jsonObject.addProperty(KEY_ALL_OPERATIONAL_INTERFACES_OP_TYPE.getValue(),remark.type());
                methodRemarks.add(jsonObject);
            }
        }
        return methodRemarks;
    }

    public static Method executeAnnotatedMethods(Class<?> clazz, String methodName) {
        try {
            // 获取类的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                // 检查方法是否被特定注解标记，并且方法名是否匹配
                if (method.isAnnotationPresent(MethodExecute.class) && method.getName().equals(methodName)) {
                    // 返回找到的方法
                    return method;
                }
            }
        } catch (SecurityException e) {
            Log.d(TAG, Objects.requireNonNull(e.getMessage()));
        }
        return null; // 如果没有找到匹配的方法，返回null
    }
}